<!-- 附件下载 -->
<template>
	<div class="filestyle" @click="download">
		{{fileName}}
	</div>
</template>

<script>
	import axios from 'axios'
	export default {
		name: "fileDownload",
		props: {
			fileName: String,
			filePath: String,
		},
		components: {},
		data() {
			return {
				headers: {
					token: window.sessionStorage.getItem('token')
				},
			};
		},
		created() {

		},
		methods: {
			download() {
				this.$message.success('附件正在下载中...')
				let params = {
					filePath: this.filePath,
					fileName: this.fileName
				}
				axios({
					method: 'post',
					url: '/member-security/file/fileDownloadCommon',
					headers: this.headers,
					data: params,
					responseType: 'blob'
				}).then(res => {
					if (!res.data) {
						return;
					}
					if ("download" in document.createElement("a")) {
						let url = window.URL.createObjectURL(new Blob([res.data]));
						let link = document.createElement("a");
						link.style.display = "none";
						link.href = url;
						if (params.fileName.indexOf("[") >= 0 && params.fileName.indexOf("]") >= 0) {
							link.setAttribute("download", params.fileName.slice(1, params.fileName.length - 1));
						} else {
							link.setAttribute("download", params.fileName);
						}
						document.body.appendChild(link);
						link.click();
					} else {
						if (params.fileName.indexOf("[") >= 0 && params.fileName.indexOf("]") >= 0) {
							navigator.msSaveBlob(new Blob([res.data]), params.fileName.slice(1, params.fileName
								.length - 1));
						} else {
							navigator.msSaveBlob(new Blob([res.data]), params.fileName);
						}
					}
				})
			},
		}
	};
</script>

<style scoped="" lang="scss">
	.filestyle {
		color: #3D80FF;
		cursor: pointer;
		// text-decoration: underline;
	}
</style>
